#!/bin/sh

set -e

if test -e "$1.cfg"
then
	cp "$1.cfg" "/Library/Application Support/UrBackup Client/var/urbackup/initial_settings.cfg"

	if test ! -e "/Library/Application Support/UrBackup Client/var/urbackup/server_idents.txt"
	then
		cp "$1.ident" "/Library/Application Support/UrBackup Client/var/urbackup/server_idents.txt"
	fi
fi

/bin/launchctl load "/Library/LaunchDaemons/org.urbackup.client.plist"
/bin/launchctl start "org.urbackup.client.backend"

if test ! -e "$1.silent"
then
	#There is user interaction required here :(
	"$2/Contents/MacOS/urbackupclientgui" register_login_item
else
	for console_user in $(ps aux | grep "loginwindow" | grep -v grep | awk '{print $1;}')
	do
		if [ "$console_user" != "root" ]
		then
			sudo -u "$console_user" "$2/Contents/MacOS/urbackupclientgui" daemon
		fi
	done
fi

"$2/Contents/MacOS/bin/buildmacOSexclusions"

/bin/launchctl load "/Library/LaunchAgents/org.urbackup.client.plist"
/bin/launchctl start "org.urbackup.client.frontend"
